import Vue from "vue";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";
import App from "./App.vue";
import router from "./router";
import axios from "axios";
import { post, get } from "@/assets/js/http.js";
import store from "./store";
import { i18n } from "./lang/index.js";
// 瀑布流
import { VueMasonryPlugin } from "vue-masonry";

Vue.use(VueMasonryPlugin);

import VueTouch from "vue-touch";

Vue.use(VueTouch, {
  name: "v-touch",
});
// import infiniteScroll from 'vue-infinite-scroll'
// Vue.use(infiniteScroll)

// 引入
import InfiniteScroll from "@/components/InfiniteScroll";
// 注册为全局组件
Vue.component("InfiniteScroll", InfiniteScroll);

Vue.prototype.$post = post;
Vue.prototype.$fetch = fetch;
Vue.prototype.$get = get;
Vue.prototype.$menuList = [];
Vue.prototype.$getPartId = function (path) {
  // console.log('this.$store.state.menuList', this.$store.state.menuList);
  let columnId = "";
  this.$store.state.menuList.forEach((menu, index) => {
    // console.log(menu.path, "-----------", menu.path === path, path);
    // console.log(menu);
    if (menu.path === path) {
      // console.log(menu.path, "-----------", path, "------columnId-----", menu.columnId);
      columnId = menu.columnId;
      return;
    }
  });
  // console.log(columnId);
  return columnId;
};
Vue.prototype.$goShopping = function () {
  window.open("https://weidian.com/?userid=1143611162", "_blank");
};
Vue.prototype.$getMenuId = function (path) {
  let menuId = "";
  this.$store.state.menuList.forEach((menu, index) => {
    if (menu.path === path) {
      menuId = menu.menuId;
      return;
    }
  });
  return menuId;
};
Vue.prototype.$goDetail = function (id) {
  // router.push({
  //   path: "/articleDetail",
  //   query: {
  //     id: id,
  //   },
  // });
  let routeUrl = this.$router.resolve({
    path: "/articleDetail", // 这里的路径就可以正常的写，不需要添加_blank: true
    query: {
      id: id,
    },
  });
  window.open(routeUrl.href, "_blank");
};
Vue.prototype.$goPage = function (path, query = {}) {
  router.push({
    path: path,
    query: query,
  });
};

Vue.prototype.$api = "https://www.zgxwfb.cn/api";
//Vue.prototype.$api = "http://8.142.116.233:8080";
// Vue.prototype.$api = "https://www.zgxwfb.cn/api";
// Vue.prototype.$api = "http://192.168.0.105:8080";

Vue.config.productionTip = false;
Vue.use(ElementUI, {
  i18n: (key, value) => i18n.t(key, value),
});
Vue.prototype.$appType = {
  name: "pc",
};

//全局前置路由守卫---初始化的时候被调用、每次路由切换的时候被调用
router.beforeEach((to, from, next) => {
  // console.log(to);
  if (to.meta.content) {
    const head = document.getElementsByTagName("head")[0];
    // 删除现有名称为 "keywords" 和 "description" 的 meta 标签
    const existingKeywordsMeta = document.querySelector('meta[name="keywords"]');
    if (existingKeywordsMeta) {
      existingKeywordsMeta.remove(); // 移除现有的关键词 meta 标签
    }
    const existingDescriptionMeta = document.querySelector('meta[name="description"]');
    if (existingDescriptionMeta) {
      existingDescriptionMeta.remove(); // 移除现有的描述 meta 标签
    }

    // 创建并设置新的关键词和描述 meta 标签
    const keywordsMeta = document.createElement("meta");
    keywordsMeta.setAttribute("name", "keywords");
    keywordsMeta.setAttribute("content", to.meta.content.keywords);
    head.appendChild(keywordsMeta); // 添加新的关键词 meta 标签

    const descriptionMeta = document.createElement("meta");
    descriptionMeta.setAttribute("name", "description");
    descriptionMeta.setAttribute("content", to.meta.content.description);
    head.appendChild(descriptionMeta); // 添加新的描述 meta 标签

    // 可选：如果需要，更新名为 "content" 的其他 meta 标签
    const contentMeta = document.querySelector('meta[name="content"]');
    if (contentMeta) {
      contentMeta.setAttribute("content", to.meta.content); // 更新 content 属性
    }
  }
  // 页面跳转/切换时 滚动条回到顶部
  document.documentElement.scrollTop = 0;
  Vue.prototype.$path = to.path;
  store.commit("setRoutePath", to.path);
  // to.path != '/' &&
  if (store.state.menuList.length === 0) {
    get(Vue.prototype.$api + "/cms/web/daoHangLan", {}).then((res) => {
      // console.log(res);
      let vueMenu = [];
      res.data.forEach((value) => {
        if (value.path == "0") {
          for (let i = 0; i < value.cnPart.length; i++) {
            vueMenu.push(value.cnPart[i]);
          }
          store.commit("setMenuList", vueMenu);

          //   // console.log(store.state.menuList);
        } else {
          vueMenu.push(value);
          store.commit("setMenuList", vueMenu);
        }
      });
      next();
      console.log(vueMenu);
    });
  } else {
    next();
  }
});

new Vue({
  router,
  store,
  axios,
  i18n,
  render: (h) => h(App),
}).$mount("#app");
